Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
When using "and" in ruby, the earth is flat

As I have always been taught in logic, the "and" operator means both values must be true, for the entire statement to be true. If you have many statements chained with "and", then any one of them being false should make the whole claim false. In Ruby, however, I ran into this scenario:

horizon_flat = true
one_up_and_down = true
magellan_fell = false
flat_earth_thesis = horizon_flat and one_up_and_down and magellan_fell

puts("Hey ruby, doesn't the horizon look flat?")
puts(horizon_flat) # true

puts("Isn't there only one up and one down?")
puts(one_up_and_down) # true

puts("Did Magellan fall off the earth?")
puts(magellan_fell) # false

puts("Is the earth flat?")
puts(flat_earth_thesis) # true

Strangely, if I just run the statement itself, it returns false correctly puts(horizon_flat and one_up_and_down and magellan_fell) # false

But if I store that statement in a variable, and later call it, the variable outputs true. Why does ruby think the earth is flat?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

While you expect this:

flat_earth_thesis = horizon_flat and one_up_and_down and magellan_fell

To be evaluated as:

flat_earth_thesis = (horizon_flat and one_up_and_down and magellan_fell)

It is instead evaluated as:

(flat_earth_thesis = horizon_flat) and one_up_and_down and magellan_fell

As noted in comments, review operator precedence.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda